package edu.gdkm.controller;

import edu.gdkm.common.Result;
import edu.gdkm.controller.request.UserPageRequest;
import edu.gdkm.entity.User;
import edu.gdkm.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//解决跨域问题
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;




    /**
     * 修改信息
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user){

        userService.update(user);
        return Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        userService.deleteById(id);
        return Result.success();
    }



    /**
     * 通过id获取用户数据 用于=>(修改,删除用户)
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }
    /**
     * 新增用户
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user){

        userService.save(user);
        return Result.success();
    }

    /**
     * 查询User表所有
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<User> users = userService.list();
        return Result.success(users);
    }

    /**
     * 分页查询 模糊查询
     * @param userPageRequest
     * @return
     */
    @GetMapping("/page")
    public Result page(UserPageRequest userPageRequest){
        return Result.success(userService.page(userPageRequest));
    }


}
